//
//  SingView.swift
//  GFQM
//
//  Created by iSolar on 2017/4/21.
//  Copyright © 2017年 iSolar. All rights reserved.
//

import UIKit

class SingView: UIScrollView ,UIScrollViewDelegate, CirCleViewDelegate {

    let KScreenWidth: CGFloat = UIScreen.main.bounds.width
    let KScreenHeight: CGFloat = UIScreen.main.bounds.height
    let topView = UIView()
    let contentView = UIView()
    let context = UIView()
    let titleLabel = UILabel()
    let numLabel = UILabel()
    let arrowV = UIImageView()
    
    var arr = ["1", "2", "3"];
    //MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentSize = CGSize(width:0,height:KScreenWidth*2)
        self.showsVerticalScrollIndicator = true
        self.showsHorizontalScrollIndicator = false
        self.isScrollEnabled = true // 可以上下滚动
        setupHeaderView()
        setupContentView()
        setupContext()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- Delegate Methods
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // 上下拉时滚动时的位移量（向上滚动时，位移是正数；向下拉动时，位移是负数）
        let offsetY = scrollView.contentOffset.y
        print("offsetY = \(offsetY)")
        // 最大位移量（注意scrollview的contentsize的高与当前视图控制器的屏幕高度的关系）
        let offsetYMax = scrollView.contentSize.height
        print("offsetYMax = \(offsetYMax)")
        let offsetYTmp = offsetYMax - self.frame.size.height
        print("offsetYTmp = \(offsetYTmp)")
        
        if 0 >= offsetY
        {
            // 向下拉
            if -100.0 >= offsetY {
                scrollView.contentInset = UIEdgeInsetsMake(80.0, 0.0, 0.0, 0.0)
            }
        } else {
            // 向上拉
            let offsetYUp = offsetY - offsetYTmp
            if 100.0 <= offsetYUp {
                scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 80.0, 0.0)
            }
        }
    }

}

extension SingView{
    fileprivate func setupHeaderView(){
        addSubview(topView)
        topView.frame = CGRect(x:0, y:0, width:KScreenWidth, height:130)
        topView.backgroundColor = UIColor.white
        let icon = UIImageView()
        topView.addSubview(icon)
        icon.frame = CGRect(x:20,y:15,width:50,height:50)
        icon.image = UIImage(named:"icon")
        icon.layer.cornerRadius = 25
        icon.layer.masksToBounds = true
        let nickName = UILabel()
        topView.addSubview(nickName)
        nickName.frame = CGRect(x:80, y:20, width:150, height:20)
        nickName.text = "行走的僵尸"
        nickName.textColor = UIColor.black
        nickName.font = UIFont.init(name: "Helvetica-Bold", size: 16)
        let la = UILabel()
        topView.addSubview(la)
        la.frame = CGRect(x:87, y:45, width:43, height:15)
        la.font = UIFont.systemFont(ofSize: 12)
        la.textColor = UIColor.black
        la.text = "练习生"
        la.textAlignment = .right 
        la.backgroundColor = UIColor.withRGBA(178, 163, 142, 0.8)

        let img1 = UIImageView()
        topView.addSubview(img1)
        img1.frame = CGRect(x:80, y:45, width:15, height:15)
        img1.image = UIImage(named:"练习生")
        img1.backgroundColor = UIColor.clear
        
        let la2 = UILabel()
        topView.addSubview(la2)
        la2.frame = CGRect(x:147, y:45, width:33, height:15)
        la2.font = UIFont.systemFont(ofSize: 12)
        la2.textColor = UIColor.black
        la2.text = "平民"
        la2.textAlignment = .right
        la2.backgroundColor = UIColor.brown
        
        let img2 = UIImageView()
        topView.addSubview(img2)
        img2.frame = CGRect(x:140, y:45, width:15, height:15)
        img2.image = UIImage(named:"平民")
        img2.backgroundColor = UIColor.clear
        
        var imageArr = NSArray()
        var titleArr = NSArray()
        var numArr = NSArray()
        imageArr = ["作品","鲜花","金币"]
        titleArr = ["作品","鲜花","金币"]
        numArr = ["0","180","0"]
        for index in 0..<imageArr.count {
            let btnView = UIView()
            btnView.frame = CGRect(x: KScreenWidth/3*CGFloat(index), y: 70, width: KScreenWidth/3, height: 50)
            topView.addSubview(btnView)
            let numL = UILabel()
            numL.frame = CGRect(x: 0, y: 10, width: KScreenWidth/3-1, height: 20)
            numL.text = numArr[index] as? String
            numL.textAlignment = .center
            numL.textColor = UIColor.lightGray
            numL.font = UIFont.systemFont(ofSize: 12)
            btnView.addSubview(numL)
            let imgV = UIImageView()
            imgV.frame = CGRect(x: KScreenWidth/6-25, y: 35, width: 20, height: 20)
            imgV.image = UIImage(named:imageArr[index] as! String)
            btnView.addSubview(imgV)
            let titleL = UILabel()
            titleL.frame = CGRect(x: KScreenWidth/6+5, y: 35, width: 30, height: 20)
            titleL.text = titleArr[index] as? String
            titleL.font = UIFont.systemFont(ofSize: 13)
            titleL.textAlignment = .center
            titleL.textColor = UIColor.black
            btnView.addSubview(titleL)
            let imgLine = UIImageView()
            btnView.addSubview(imgLine)
            if (imageArr.count == 2) {
                imgLine.image = UIImage(named:"")
            }else {
                imgLine.image = UIImage(named:"userLine")
            }
            imgLine.frame = CGRect(x: KScreenWidth/3, y: 10, width: 1, height: 40)
            
            let btn = UIButton()
            btn.frame = btnView.frame
            topView.insertSubview(btn, aboveSubview: btnView)
            btn.addTarget(self, action: #selector(SingView.btnClick), for: .touchUpInside)
        }
        
    }

    fileprivate func setupContentView(){
        addSubview(contentView)
        contentView.frame = CGRect(x: 0, y: 140, width: KScreenWidth, height: 180)
        contentView.backgroundColor = UIColor.white
        //定义collectionView的布局类型，流布局
        let layout = UICollectionViewFlowLayout()
        //设置cell的大小
        layout.itemSize = CGSize(width: KScreenWidth - 160, height: 100)
        //滑动方向 默认方向是垂直
        layout.scrollDirection = .horizontal
        //每个Item之间最小的间距
        layout.minimumInteritemSpacing = 10
        //每行之间最小的间距
        layout.minimumLineSpacing = 10
        //定义一个UICollectionView
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 100), collectionViewLayout: layout)
        //设置collectionView的背景颜色
        collectionView.backgroundColor = UIColor.white
        contentView.addSubview(collectionView)
        //设置collectionView的代理和数据源
        collectionView.isScrollEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        //CollectionViewCell的注册
        collectionView.register(UINib(nibName: "SingCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MyCell")
        
        let view = UIView()
        contentView.addSubview(view)
        var array = NSArray()
        var titleArray = NSArray()
        array = ["签到有礼","歌手","HiFi音乐"]
        titleArray = ["好友动态","K歌比赛","伐木累"]
        
        view.frame = CGRect(x: 0, y: 110, width: KScreenWidth, height: 60)
        for index in 0..<array.count {
            let btnView = UIButton()
            btnView.frame = CGRect(x: KScreenWidth/3*CGFloat(index), y: 0, width: KScreenWidth/3, height: 60)
            view.addSubview(btnView)
            btnView.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
            let image = UIImageView()
            image.frame = CGRect(x: (KScreenWidth/3-40)/2, y: 0, width: 40, height: 40)
            image.image = UIImage(named:array[index] as! String)
            btnView.addSubview(image)
            let label = UILabel()
            label.frame = CGRect(x: (KScreenWidth/3-80)/2, y: 40, width: 80, height: 20)
            label.font = UIFont.systemFont(ofSize: 15)
            label.textColor = UIColor.black
            label.textAlignment = .center
            label.text = titleArray[index] as? String
            btnView.addSubview(label)
        }
        
    }
    
    fileprivate func setupContext(){
        addSubview(context)
        context.frame = CGRect(x: 0, y: 370, width: KScreenWidth, height: (KScreenWidth-80)/3+40)
        context.backgroundColor = UIColor.white
        let view = UIView()
        addSubview(view)
        view.frame = CGRect(x: 0, y: 330, width: KScreenWidth, height: 40)
        titleLabel.frame = CGRect(x: (KScreenWidth-80)/2, y: 10, width: 80, height: 20)
        view.addSubview(titleLabel)
        titleLabel.text = "热门作品"
        titleLabel.textColor = UIColor.black
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.init(name: "Helvetica-Bold", size: 17)
        
        arrowV.frame = CGRect(x:KScreenWidth-30, y:13, width:15, height:15)
        view.addSubview(arrowV)
        arrowV.image = UIImage(named:"arrow")
        
        numLabel.frame = CGRect(x: KScreenWidth/2+50, y: 10, width: KScreenWidth/2 - 80, height: 20)
        view.addSubview(numLabel)
        numLabel.textAlignment = .right
        numLabel.textColor = UIColor.lightGray
        numLabel.text = "看榜单"
        numLabel.font = UIFont.systemFont(ofSize: 12)
        
        var array = NSArray()
        var titleArray = NSArray()
        array = ["img1","img2","img3"]
        titleArray = ["爱情海","咏春","明天会更好"]
        for index in 0..<array.count {
            let btn = UIButton()
            btn.frame = CGRect(x:20 + (20+(KScreenWidth-80)/3)*CGFloat(index), y: 0, width: (KScreenWidth-80)/3, height: (KScreenWidth-80)/3+40)
            btn.setImage(UIImage(named:array[index] as! String), for: .normal)
            btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
            btn.imageEdgeInsets = UIEdgeInsetsMake(0,0,40,0)
            let la = UILabel()
            la.frame = CGRect(x: 0, y: (KScreenWidth-80)/3, width: (KScreenWidth-80)/3, height: 40)
            la.font = UIFont.systemFont(ofSize: 15)
            la.text = titleArray[index] as? String
            la.textColor = UIColor.lightGray
            la.numberOfLines = 0
            btn.addSubview(la)
            context.addSubview(btn)
        }
    }
    
}

extension SingView {
    @objc fileprivate func btnClick (){
        print("---------------------------")
    }
}

extension SingView: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    // #MARK: --UICollectionViewDataSource的代理方法
    /**
     - 该方法是可选方法，默认为1
     - returns: CollectionView中section的个数
     */
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    /**
     - returns: Section中Item的个数
     */
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arr.count
    }
    
    /**
     - returns: 绘制collectionView的cell
     */
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! SingCollectionViewCell
        
        cell.imageView.image = UIImage(named: arr[indexPath.item])
        
        return cell
    }
    
    // #MARK: --UICollectionViewDelegate的代理方法
    /**
     Description:当点击某个Item之后的回应
     */
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("(\(indexPath.section),\(indexPath.row))")
    }
    
    /**
     Description:可以定制不同的item
     
     - returns: item的大小
     */
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: KScreenWidth - 160, height: 100)
    }
    
}


